package com.zhuang.chapter15;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class FileSupple {

	public static void main(String[] args) {
		suppleFile();
	}

	public static void suppleFile() {
		System.out.println("请输入源文件名 例如 f:\\\\1.txt");
		Scanner scanner = new Scanner(System.in);
		String srcFilePath = scanner.nextLine();
		System.out.println("请输入目标文件名 例如 f:\\\\1.txt");
		String desFilePath = scanner.nextLine();

		byte[] buf = new byte[1024];
		int readData = 0;

		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		try {
			fileInputStream = new FileInputStream(srcFilePath);
			fileOutputStream = new FileOutputStream(desFilePath, true);
			/*
			 * 从输入流最多读取b.length字节的数据到字节数组 返回-1 读取完毕 读取正常 返回实际读取的字节数
			 */
			while ((readData = fileInputStream.read(buf)) != -1) {
				String str = new String(buf, 0, readData);
				// 显示
				System.out.println(new String(buf, 0, readData));
				fileOutputStream.write(str.getBytes("UTF-8"));
			}
			System.out.println("文件追加成功");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				scanner.close();
				fileInputStream.close();
				fileOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
